package org.example;

public class MyQueue {
    // 采用带尾指针的单链表实现的队列
    static class ListNode {
        private int val;
        private ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }


    private int usedSize;
    private ListNode head;
    private ListNode tail;
    //入队列操作
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            tail = head;
        }else {
            tail.next = node;
            tail = node;
        }
        usedSize++;
    }

    //出队列操作
    public int poll() {
        if (head == null) {
            return -1;
        }
        int val = head.val;
        if (head.next == null) {
            head = null;
            tail = null;
        }else {
            head = head.next;
        }
        usedSize--;
        return val;
    }
    //获得队头元素
    public int peek() {
        if (head == null) {
            return -1;
        }
        return head.val;
    }
}
